package com.bugslog.api.base;

import com.bugslog.framework.common.util.ShaUtil;
import com.bugslog.framework.vo.BaseVO;
import com.bugslog.framework.web.annotation.Api;
import com.bugslog.framework.web.annotation.SessionRequire;
import com.bugslog.framework.web.api.ApiResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/session")
public class SessionApi extends TheApi{
	
//	@Autowired private UserService userService;
	
	@ResponseBody
	@RequestMapping(value="logout")
	@Api
	public ApiResponse logout(){
		super.setSession(null);
		return ApiResponse.success("good bye");
		
	}
	
	@Api @ResponseBody @RequestMapping(value="login",method=RequestMethod.POST)
	 ApiResponse login(@RequestBody LoginForm form){
		if(form == null){
			return ApiResponse.illegal();
		}
		String error = form.validate();
		if(error != null){
			return ApiResponse.error(error);
		}
		
		String password = form.getPassword();
		password = ShaUtil.hash(password);
		
		/*User real = this.userService.getByUsernameAndPassword(form.getUsername(), password);
		if(real == null){
			return ApiResponse.error("密码不正确");
		}*/
		
		TheApiSession session = new TheApiSession();
//		session.setNickname(real.getName());
//		session.setId(real.getId());
		return 	this.onSessionOk(session);			
		
	}
	
	@SessionRequire @Api @ResponseBody @RequestMapping("touch")
	ApiResponse touch(){
		
		TheApiSession session = super.getSession();
		return this.onSessionOk(session);		
	}
	
	private ApiResponse onSessionOk(TheApiSession session){				

		super.setSession(session);
		return ApiResponse.success("欢迎回来").putValue("data",this.makeDataAfterLogined(session));
	}
	
	private Map<String, Object> makeDataAfterLogined(TheApiSession session){
		Map<String, Object> data = new HashMap();
		data.put("session",session);
		return data;
	}
	

}

class LoginForm extends BaseVO{
	
	private String username;
	private String password;



	public String getUsername() {
		return username;
	}



	public void setUsername(String username) {
		this.username = username;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	@Override
	public String validate() {
		if(this.username == null || this.username.length() < 2){
			return "请填写账号";
		}
		if(this.password  == null || this.password.length() < 1){
			return "请输入密码";
		}
		return null;
	}
}


class MenuUtil {
	public static Map<String,String > newMenu (String uiSref,String alias){
		Map<String,String> one = new HashMap<String,String>();
		one.put("ui-sref", uiSref);
		one.put("alias", alias);
		return one;
	}
	
	public static Map<String,Object> newGroup(String alias, List<Map<String,String>> submenus){
		Map<String,Object> group = new HashMap<String,Object>();
		group.put("alias", alias);
		group.put("submenus",submenus);
		return group;
	}
}


